package com.atguigu.coupon.coupons.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.coupon.coupons.dao.SmsSeckillSessionDao;
import com.atguigu.coupon.coupons.entity.SmsSeckillSessionEntity;
import com.atguigu.coupon.coupons.service.SmsSeckillSessionService;


@Service("smsSeckillSessionService")
public class SmsSeckillSessionServiceImpl extends ServiceImpl<SmsSeckillSessionDao, SmsSeckillSessionEntity> implements SmsSeckillSessionService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SmsSeckillSessionEntity> page = this.page(
                new Query<SmsSeckillSessionEntity>().getPage(params),
                new QueryWrapper<SmsSeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询需要秒杀的活动场次以及对应的商品信息
     *
     * @return
     */
    public List<SmsSeckillSessionEntity> SecToCoupon() {
        String now = DateUtil.now();
        Date date = DateUtil.parse(now);

        //一天的开始，结果：2017-03-01 00:00:00
        Date beginOfDay = DateUtil.beginOfDay(date);
        String start = DateUtil.format(beginOfDay, "yyyy/MM/dd HH:mm:ss");
        Date newDate = DateUtil.offset(date, DateField.DAY_OF_MONTH, 2);
        Date endOfDay1 = DateUtil.endOfDay(newDate);
        String end = DateUtil.format(endOfDay1, "yyyy/MM/dd HH:mm:ss");
        List<SmsSeckillSessionEntity> entities = this.baseMapper.SecToCoupon(start,end);

        return entities;
    }

}